﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace HackingSilverlightLibrary
{ 
    public class FX : Control
    { 
        public FX()
        {
            Flip = false;
            AutoFlip = false;
        }

        public override void OnApplyTemplate()
        {
            if (Flip)
            {
                if (AutoFlip)
                {
                    ((Grid)GetTemplateChild("ContentPresenters")).MouseLeftButtonDown += new MouseButtonEventHandler(FXFlip_MouseLeftButtonDown);
                }
            }
            base.OnApplyTemplate();
        }
 
        #region // Flip Effect 

        private bool _FXFront = true;

        void FXFlip_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ExecuteFlip();
        }

        public void ExecuteFlip()
        {
            if (Flip)
            {
                VisualStateManager.GoToState(this, ((_FXFront) ? "FlipState1" : "FlipState2"), true);
                _FXFront = !_FXFront;
            }
        }

        public static readonly DependencyProperty AutoFlipProperty = DependencyProperty.Register("AutoFlip", typeof(bool), typeof(FX), new PropertyMetadata(new PropertyChangedCallback(OnAutoFlipChanged)));

        public bool AutoFlip
        {
            get { return (bool)GetValue(AutoFlipProperty); }
            set { SetValue(AutoFlipProperty, value); }
        }

        private static void OnAutoFlipChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            FX ThisElement = DpObj as FX;

            if (ThisElement != null)
            {
                ThisElement.Flip = true; 
            }
        } 

        public static readonly DependencyProperty FlipProperty = DependencyProperty.Register("Flip", typeof(bool), typeof(FX), new PropertyMetadata(new PropertyChangedCallback(OnFlipChanged)));

        public bool Flip
        {
            get { return (bool)GetValue(FlipProperty); }
            set { SetValue(FlipProperty, value); }
        }
 
        private static void OnFlipChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            FX ThisElement = DpObj as FX;

            if (ThisElement != null)
            {
                ThisElement.DefaultStyleKey = typeof(FX); 
            }
        } 

        public static readonly DependencyProperty FrontProperty = DependencyProperty.Register("Front", typeof(UIElement), typeof(FX), new PropertyMetadata(null));

        public UIElement Front
        {
            get { return (UIElement)GetValue(FrontProperty); }
            set { SetValue(FrontProperty, value); }
        }

        public static readonly DependencyProperty BackProperty = DependencyProperty.Register("Back", typeof(UIElement), typeof(FX), new PropertyMetadata(null));

        public UIElement Back
        {
            get { return (UIElement)GetValue(BackProperty); }
            set { SetValue(BackProperty, value); }
        }

        #endregion 
  
    }
}
